import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Created by Scott Tiger.
 * User: tomaszsocha
 * Date: 2010-05-05
 * Time: 21:15:08
 */
public class LaczenieZk extends JPanel{
    private ItemZk first;
    private ItemZk second;
    private JButton nowy;
    private JPanel main;
    private JPanel pFilter;
    private JFileChooser fc;
    private Polaczenia p;
    private JPanel fPref;
    private JPanel fSuf;
    private JComboBox pref;
    private JComboBox sufy;
    private HashMap <String,Object> prefFilter = new HashMap<String,Object>();
    private HashMap <String,Object> sufFilter =  new HashMap<String,Object>();
    private HashMap <String,Integer> wagi =  new HashMap<String,Integer>();
    public static Collection<Item> zbrojownia= new LinkedList();
    public static Zk zk;
    DefaultListModel model = new DefaultListModel();
    ListCellRenderer lcr = new ItemRenderer();


    public LaczenieZk(final String itemName,Boolean itemOnly,Polaczenia pol){

        this.setLayout(new GridLayout(1,0));
        p = pol;


         main = new JPanel();
         nowy = new JButton();

         this.add(main);

        Action polacz = new AbstractAction("Polacz") {
            public void actionPerformed(ActionEvent actionEvent) {
               String newone = p.polacz((String)first.getSelectedItem(),(String)second.getSelectedItem(),"Armor");
               nowy.setText(newone);
               nowy.setVisible(true);              
/*             first.remove(first.getSelectedIndex());
               second.remove(second.getSelectedIndex());
               first.addItem(newone);
               second.addItem(newone);
               first.setVisible(true);
               second.setVisible(true);*/
            }
        };


        Action next = new AbstractAction("Open") {

          public void actionPerformed(ActionEvent actionEvent) {

             int returnVal = fc.showOpenDialog(LaczenieZk.this);
             if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                //This is where a real application would open the file.
                first = new ItemZk(file);
                second = new ItemZk(file);
                 main.add(first);
                 main.add(second);
                 main.add(nowy);
                 nowy.setVisible(false);
                //LaczenieZk.this.add(new ItemZk(file));

            }
          }
        };
        JButton open = new JButton(next);
        JButton merge = new JButton(polacz);
        fc = new JFileChooser();
        pFilter = new JPanel();
        pFilter.setSize(200,200);        
        pFilter.add(open);
        pFilter.add(merge);
        this.add(pFilter);

    }

    public LaczenieZk(final LaczenieItem item, boolean itemOnly, Polaczenia p) {
        this.setLayout(new GridLayout(1,1));
        this.p = p;
         pref = new Prefix(item.getName());
         sufy = new Sufix(item.getName());
         pFilter = new JPanel(new FlowLayout());
         main = new JPanel();
         main.setLayout(new BoxLayout(main, BoxLayout.X_AXIS));         

         fPref = new JPanel();//(new GridLayout(1,0));
         fPref.setLayout(new BoxLayout(fPref, BoxLayout.Y_AXIS));
         fSuf = new JPanel();//new GridLayout(1,0));
         fSuf.setLayout(new BoxLayout(fSuf, BoxLayout.Y_AXIS));
         Action aSuf = new AbstractAction("+") {
             public void actionPerformed(ActionEvent actionEvent) {
                 if (!sufFilter.containsKey(sufy.getSelectedItem().toString()))
                 {
                 JButton nowySuf = new JButton(sufy.getSelectedItem().toString());
                 fSuf.add(nowySuf);
                 main.getParent().getParent().repaint();
                 sufFilter.put(sufy.getSelectedItem().toString(),nowySuf);

                 }    
             }
         };
         Action rSuf = new AbstractAction("-") {
             public void actionPerformed(ActionEvent actionEvent) {
                if (sufFilter.containsKey(sufy.getSelectedItem().toString()))
                 {
                 fSuf.remove((JButton) sufFilter.get(sufy.getSelectedItem()));
                 main.getParent().getParent().repaint();
                 sufFilter.remove(sufy.getSelectedItem().toString());
                 }
             }
         };
         Action aPref = new AbstractAction("+") {
             public void actionPerformed(ActionEvent actionEvent) {
                 if (!prefFilter.containsKey(pref.getSelectedItem().toString()))
                 {
                 JButton nowyPref = new JButton(pref.getSelectedItem().toString());
                 fPref.add(nowyPref);
                 main.getParent().getParent().repaint();
                 prefFilter.put(pref.getSelectedItem().toString(),nowyPref);

                 }
             }
         };

         Action rPref = new AbstractAction("-") {
             public void actionPerformed(ActionEvent actionEvent) {
                if (prefFilter.containsKey(pref.getSelectedItem().toString()))
                 {
                 fPref.remove((JButton) prefFilter.get(pref.getSelectedItem()));
                 main.getParent().getParent().repaint();
                 prefFilter.remove(pref.getSelectedItem().toString());
                 }
             }
         };


         Action otwieraj = new AbstractAction("Open") {

          public void actionPerformed(ActionEvent actionEvent) {
             zk = new Zk(main,item,getpreffilter(),getsuffilter());
             int returnVal = fc.showOpenDialog(LaczenieZk.this);
             if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                //This is where a real application would open the file.
                 try {
                     wagi.putAll(item.getLaczeniaItem().getWagi());
                     wagi.putAll(item.getLaczeniaSufy().getWagi());
                     wagi.putAll(item.getLaczeniaPrefy().getWagi());

                     //zbrojownia = zk.initZbrojownia(file,zbrojownia,wagi);
                     zbrojownia = zk.initZbrojownia(file,zbrojownia,wagi,model,item);
                 } catch (FileNotFoundException e) {
                     e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                 }
                 //zk.polaczItem(zbrojownia,zbrojownia.size());

                //LaczenieZk.this.add(new ItemZk(file));

            }
          }
        };

         Action polacz = new AbstractAction("Polacz") {
            public void actionPerformed(ActionEvent actionEvent) {
               zk.polaczItem(zbrojownia,zbrojownia.size()-2);

            }
        };

        JButton open = new JButton(otwieraj);
        JButton merge = new JButton(polacz);

        fc = new JFileChooser();
        
        //pFilter.add(open);
        //pFilter.add(merge);

        JPanel pZK = new JPanel(new GridLayout(10,1));
        pZK.add(open);
        pZK.add(merge);

         JButton addPref = new JButton(aPref);
         JButton addSuf = new JButton(aSuf);
         JButton remPref = new JButton(rPref);
         JButton remSuf = new JButton(rSuf);

        JList dragFrom;
        dragFrom = new JList(model);
        dragFrom.setCellRenderer(lcr);
        dragFrom.setDragEnabled(true);
        dragFrom.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JLabel label = new JLabel("Drag from here:");
        label.setAlignmentX(0f);
        JScrollPane sp = new JScrollPane(dragFrom);
        JPanel pa = new JPanel();
        pa.add(label);
        sp.setAlignmentX(0f);
        pa.add(sp);
        add(pa, BorderLayout.WEST);


         pFilter.add(pref);
         pFilter.add(addPref);
         pFilter.add(remPref);
         pFilter.add(sufy);
         pFilter.add(addSuf);
         pFilter.add(remSuf);
         pFilter.setSize(300,300);
         main.add(fPref);
         main.setSize(300,300);
         main.add(fSuf);
         this.add(pFilter);
         this.add(pZK);
         this.add(main);
         this.add(pa);
         

    }

    public String getpreffilter(){
        String result = "";
        Iterator it  = prefFilter.keySet().iterator();
        while (it.hasNext()){
            String key = (String)it.next();
            result += "|"+key;
        }
        return result;
    }

    public String getsuffilter(){
        String result = "";
        Iterator it  = sufFilter.keySet().iterator();
        while (it.hasNext()){
            String key = (String)it.next();
            result += "|"+key;
        }
        return result;
    }


}
